﻿//准备ExtJS环境
Ext.require
(
    [
        'Ext.form.field.File',
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.state.*',
        'Ext.form.*'
    ]
);

//定义用户表格控件
Ext.define
(
    'Project.Grid',
    {
        extend: 'Ext.grid.Panel',

        //编辑窗体
        win_edit: null,

        //新增窗体
        win_create: null,

        //附件管理窗体
        win_attachment: null,

        //用于保存上传对象
        swf_uploader: null,

        //初始化组件
        initComponent: function ()
        {
            Ext.apply
            (
                this,
                {
                    listeners:
                    {
                        //设置两个指头的滚动
                        render: Common.ScrollableRender
                    },
                    dockedItems:
                    [
                        {
                            xtype: 'toolbar',
                            height: 36,
                            items:
                            [
                                {
                                    iconCls: 'icon-refresh',
                                    text: '刷新',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '刷新数据',
                                    handler: this.onRefreshClick
                                },
                                '-',
                                {
                                    iconCls: 'icon-add',
                                    text: '新增',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '新增工程项目',
                                    handler: this.onAddClick
                                },
                                {
                                    iconCls: 'icon-edit',
                                    text: '编辑',
                                    itemId: 'button-edit',
                                    disabled: true,
                                    hidden: false,
                                    scope: this,
                                    tooltip: '编辑工程项目',
                                    handler: this.onEditClick
                                },
                                {
                                    iconCls: 'icon-delete',
                                    text: '删除',
                                    disabled: true,
                                    hidden: false,
                                    itemId: 'button-delete',
                                    scope: this,
                                    tooltip: '删除工程项目',
                                    handler: this.onDeleteClick
                                },
                                {
                                    iconCls: 'icon-upload',
                                    text: '管理附件',
                                    itemId: 'button-upload',
                                    scope: this,
                                    disabled: true,
                                    hidden: false,
                                    tooltip: '管理项目相关附件',
                                    handler: this.onUploadClick
                                },
                                '-',
                                {
                                    iconCls: 'icon-view',
                                    text: '查看',
                                    itemId: 'button-view',
                                    disabled: true,
                                    hidden: false,
                                    scope: this,
                                    tooltip: '查看工程项目详细信息',
                                    handler: this.onViewClick
                                },
                                '->',
                                {
                                    iconCls: 'icon-submit',
                                    text: '提交',
                                    itemId: 'button-submit',
                                    scope: this,
                                    disabled: true,
                                    hidden: false,
                                    tooltip: '提交工程项目审核申请',
                                    handler: this.onSubmitClick
                                }
                            ]
                        }
                    ]
                }
            );
            this.callParent();
            this.getSelectionModel().on('selectionchange', this.onSelectionChange, this);
        },

        //处理选中项目变化事件
        onSelectionChange: function (selectionModel, selections)
        {
            //判断是否选中项目
            if (selections.length === 0)
            {
                //禁用删除按钮
                this.down('#button-delete').setDisabled(true);
                //禁用编辑按钮
                this.down('#button-edit').setDisabled(true);
                //禁用上传按钮
                this.down('#button-upload').setDisabled(true);
                //禁用查看按钮
                this.down('#button-view').setDisabled(true);
                //禁用提交按钮
                this.down('#button-submit').setDisabled(true);
            }
            else
            {
                //启用删除按钮
                this.down('#button-delete').setDisabled(false);
                //启用编辑按钮
                this.down('#button-edit').setDisabled(false);
                //启用上传按钮
                this.down('#button-upload').setDisabled(false);
                //启用查看按钮
                this.down('#button-view').setDisabled(false);
                //启用提交按钮
                this.down('#button-submit').setDisabled(false);
            }
        },

        //文件上传成功回调
        onFileUploadSuccess: function (response, options)
        {
            //判定是否有所属编号
            if (options.OwnerID)
            {
                //如果有所属编号，则刷新附件数据
                store_attachment.load();
            }
            else
            {
                //判定是否成功
                if (response.success)
                {
                    //添加到集合
                    store_files.add(response.data);
                }
            }
        },

        //管理附件
        onManageAttachment: function (projectID)
        {
            //判定窗体是否创建
            if (!this.win_attachment)
            {
                //创建表格
                var grid_attachment = new Ext.grid.GridPanel
                (
                    {
                        store: store_attachment,
                        border: 0,
                        stateful: true,
                        height: '100%',
                        width: '100%',
                        viewConfig:
                        {
                            stripeRows: true,
                            loadingText: '数据加载中...',
                            singleSelect: true
                        },
                        columns:
                        [
                            {
                                text: '附件名称',
                                flex: 1,
                                sortable: false,
                                align: 'left',
                                dataIndex: 'AttachmentName',
                                hideable: false
                            },
                            {
                                text: '公开附件',
                                width: 80,
                                sortable: false,
                                align: 'center',
                                renderer: renderGridReadonlyCheckBox,
                                dataIndex: 'IsOpen',
                                hideable: false
                            },
                            {
                                text: '文件类型',
                                width: 80,
                                sortable: false,
                                align: 'left',
                                dataIndex: 'FileExtension',
                                hideable: false
                            }
                        ],
                        dockedItems:
                        [
                            {
                                xtype: 'toolbar',
                                height: 36,
                                items:
                                [
                                    {
                                        iconCls: 'icon-add',
                                        text: '上传附件',
                                        scope: this,
                                        hidden: false,
                                        tooltip: '上传项目附件',
                                        handler: function ()
                                        {
                                            //获得表格控件
                                            var grid = Ext.getCmp('project-grid');

                                            //上传附件
                                            UploadFile
                                            (
                                                {
                                                    title: '上传附件',
                                                    mode: 1,
                                                    FileType: 1,
                                                    file_size_limit: attachment_file_size_limit,
                                                    file_size_limit_tip: attachment_file_size_limit_tip,
                                                    file_types: attachment_file_types,
                                                    file_types_description: attachment_file_types_description,
                                                    url: Common.MakeControllerUrl('FileUpload', 'UploadTempFile'),
                                                    callback: grid.onFileUploadSuccess,
                                                    OwnerID: store_attachment.projectID
                                                }
                                            );
                                        }
                                    },
                                    {
                                        iconCls: 'icon-delete',
                                        itemId: 'attachment-delete',
                                        text: '删除附件',
                                        scope: this,
                                        hidden: false,
                                        disabled: true,
                                        tooltip: '删除项目附件',
                                        handler: function (button)
                                        {
                                            //获得选中的行
                                            var selection = button.up('grid').getView().getSelectionModel().getSelection()[0];

                                            //判断是否选中行
                                            if (selection)
                                            {
                                                //询问是否确定删除
                                                Ext.Msg.show
                                                (
                                                    {
                                                        title: '删除附件',
                                                        width: 320,
                                                        height: 240,
                                                        msg: '确定要删除选中的附件"' + selection.data.AttachmentName + '?',
                                                        buttons: Ext.Msg.YESNO,
                                                        icon: Ext.Msg.QUESTION,
                                                        fn: function (button)
                                                        {
                                                            //判断是否确定删除 
                                                            if (button == 'yes')
                                                            {
                                                                //创建请求对象
                                                                Ext.Ajax.request
                                                                (
                                                                    {
                                                                        url: Common.MakeAbsoluteUrl('DeleteAttachment'),
                                                                        method: HttpMethod.Post,
                                                                        params: selection.data,
                                                                        callback: function (options, success, response)
                                                                        {
                                                                            //处理错误
                                                                            if (Common.HandleError(options, success, response))
                                                                            {
                                                                                //刷新数据
                                                                                store_attachment.load();
                                                                            }
                                                                        }
                                                                    }
                                                                );
                                                            }
                                                        }
                                                    }
                                                );
                                            }
                                        }
                                    }
                                ]
                            }
                        ],
                        listeners:
                        {
                            selectionchange: function (selectionModel, selections)
                            {
                                //判断是否选中项目
                                if (selections.length === 0)
                                {
                                    //禁用删除按钮
                                    this.down('#attachment-delete').setDisabled(true);
                                }
                                else
                                {
                                    //禁用删除按钮
                                    this.down('#attachment-delete').setDisabled(false);
                                }
                            },
                            //设置两个指头的滚动
                            render: Common.ScrollableRender
                        }
                    }
                 );

                //创建窗体
                this.win_attachment = new Ext.Window
                (
                    {
                        title: '项目附件管理',
                        maximizable: false,
                        maximized: false,
                        width: 540,
                        height: 360,
                        bodyBorder: false,
                        draggable: true,
                        isTopContainer: true,
                        modal: true,
                        resizable: false,
                        bodyPadding: 0,
                        closeAction: 'hide',
                        closable: true,
                        layout: 'fit',
                        items:
                        [
                            grid_attachment
                        ]
                    }
                );
            }

            //设置代理地址
            store_attachment.proxy.url = Common.MakeAbsoluteUrl('Attachments?projectID=' + projectID);

            //设置项目编号
            store_attachment.projectID = projectID;

            //加载数据
            store_attachment.load();

            //显示窗体
            this.win_attachment.show();
        },

        //处理删除按钮点击事件
        onDeleteClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if (selection)
            {
                //询问是否确定删除
                Ext.Msg.show
                (
                    {
                        title: '删除工程',
                        width: 320,
                        height: 240,
                        msg: '确定要删除选中的工程项目"' + selection.data.ProjectName + '"吗?',
                        buttons: Ext.Msg.YESNO,
                        icon: Ext.Msg.QUESTION,
                        fn: function (button)
                        {
                            //判断是否确定删除 
                            if (button == 'yes')
                            {
                                //创建请求对象
                                Ext.Ajax.request
                                (
                                    {
                                        url: Common.MakeAbsoluteUrl('Delete'),
                                        method: HttpMethod.Post,
                                        params: selection.data,
                                        callback: function (options, success, response)
                                        {
                                            //处理错误
                                            if (Common.HandleError(options, success, response))
                                            {
                                                //刷新数据
                                                store.load();
                                            }
                                        }
                                    }
                                );
                            }
                        }
                    }
                );
            }
        },

        //处理刷新按钮点击事件
        onRefreshClick: function ()
        {
            //刷新数据
            this.store.load();
        },

        //处理新增按钮点击事件
        onAddClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //判定窗体是否创建
            if (!this.win_create)
            {
                //创建附件表格
                var grid_files = Ext.create
                (
                    'Ext.grid.Panel',
                    {
                        store: store_files,
                        flex: 1,
                        columns:
                        [
                            {
                                text: '附件名称',
                                width: 160,
                                minWidth: 160,
                                flex: 1,
                                sortable: false,
                                align: 'left',
                                dataIndex: 'DisplayName',
                                hideable: false
                            },
                            {
                                text: '公开附件',
                                width: 80,
                                sortable: false,
                                align: 'center',
                                renderer: renderGridReadonlyCheckBox,
                                dataIndex: 'IsOpen',
                                hideable: false
                            },
                            {
                                xtype: 'actioncolumn',
                                width: 24,
                                sortable: false,
                                hideable: false,
                                align: 'center',
                                items:
                                [
                                    {
                                        icon: grid_delete_image_url,
                                        tooltip: '删除附件',
                                        handler: function (grid, rowIndex, colIndex)
                                        {
                                            //获得选中的记录
                                            var record = grid.getStore().getAt(rowIndex);

                                            //询问是否删除
                                            Ext.Msg.show
                                            (
                                                {
                                                    title: '删除附件',
                                                    width: 320,
                                                    height: 240,
                                                    msg: '确定要删除选中的附件"' + record.data.DisplayName + '"吗?',
                                                    buttons: Ext.Msg.YESNO,
                                                    icon: Ext.Msg.QUESTION,
                                                    fn: function (button)
                                                    {
                                                        //判断是否确定删除 
                                                        if (button == 'yes')
                                                        {
                                                            //删除记录
                                                            store_files.remove(record);
                                                        }
                                                    }
                                                }
                                            );
                                        }
                                    }
                                ]
                            }
                        ],
                        height: 150
                    }
                 );

                //创建窗体
                this.win_create = new Ext.Window
                (
                    {
                        title: '新增工程项目',
                        maximizable: false,
                        maximized: false,
                        width: 480,
                        height: 600,
                        bodyBorder: false,
                        draggable: true,
                        isTopContainer: true,
                        modal: true,
                        resizable: false,
                        bodyPadding: 0,
                        closeAction: 'hide',
                        closable: false,
                        layout: 'fit',
                        fieldDefaults:
                        {
                            labelAlign: 'left',
                            labelWidth: 72,
                            anchor: '100%'
                        },
                        items:
                        [
                            {
                                xtype: 'form',
                                method: 'POST',
                                waitMsgTarget: true,
                                waitTitle: Common.ApplicationName,
                                frame: false,
                                bodyPadding: 12,
                                bodyStyle: 'background-color:White;margin:0;',
                                bodyBorder: false,
                                border: 0,
                                autoScroll: true,
                                defaultType: 'textfield',
                                autoScroll: false,
                                fieldDefaults:
                                {
                                    labelAlign: 'left',
                                    labelWidth: 72,
                                    anchor: '100%',
                                    msgTarget: 'qtip',
                                    autoFitErrors: true,
                                    disabledCls: ''
                                },
                                items:
                                [
                                    {
                                        xtype: 'textfield',
                                        name: 'ProjectName',
                                        fieldLabel: '项目名称',
                                        emptyText: '80个字符',
                                        allowBlank: false,
                                        maxLength: 80
                                    },
                                    {
                                        xtype: 'combobox',
                                        name: 'CategoryID',
                                        itemId: 'cboCategory',
                                        fieldLabel: '项目类别',
                                        emptyText: '项目类别',
                                        store: store_master_category,
                                        displayField: 'Name',
                                        valueField: 'ID',
                                        queryMode: 'local',
                                        hideTrigger: false,
                                        forceSelection: true,
                                        typeAhead: true,
                                        autocomplete: true,
                                        allowBlank: false
                                    },
                                    {
                                        xtype: 'combobox',
                                        name: 'Roadway',
                                        itemId: 'cboRoadway',
                                        fieldLabel: '线路名称',
                                        emptyText: '项目线路名称',
                                        store: store_master_roadway,
                                        displayField: 'PinYin',
                                        valueField: 'Name',
                                        queryMode: 'local',
                                        hideTrigger: false,
                                        forceSelection: false,
                                        typeAhead: true,
                                        autocomplete: true,
                                        allowBlank: false,
                                        enableKeyEvents: true
                                    },
                                    {
                                        xtype: 'textfield',
                                        name: 'BeginStake',
                                        fieldLabel: '起点桩号',
                                        emptyText: '项目起点桩号',
                                        allowBlank: false,
                                        maxLength: 40
                                    },
                                    {
                                        xtype: 'textfield',
                                        name: 'EndStake',
                                        fieldLabel: '终点桩号',
                                        emptyText: '项目终点桩号',
                                        allowBlank: false,
                                        maxLength: 40
                                    },
                                    {
                                        xtype: 'textfield',
                                        name: 'GrantNumber',
                                        fieldLabel: '项目批号',
                                        emptyText: '项目文件批号',
                                        allowBlank: true,
                                        maxLength: 80
                                    },
                                    {
                                        xtype: 'datefield',
                                        name: 'StartDate',
                                        id: 'txtStartDate',
                                        fieldLabel: '开工日期',
                                        emptyText: '项目开工日期',
                                        format: 'Y/m/d',
                                        allowBlank: true,
                                        vtype: 'dateRange',
                                        endDateField: 'txtCompletionDate'
                                    },
                                    {
                                        xtype: 'datefield',
                                        name: 'CompletionDate',
                                        id: 'txtCompletionDate',
                                        fieldLabel: '竣工日期',
                                        emptyText: '项目竣工日期',
                                        format: 'Y/m/d',
                                        allowBlank: true,
                                        vtype: 'dateRange',
                                        beginDateField: 'txtStartDate'
                                    },
                                    {
                                        xtype: 'textfield',
                                        name: 'ProjectLeader',
                                        fieldLabel: '项目负责',
                                        emptyText: '项目负责人姓名',
                                        allowBlank: true,
                                        maxLength: 20
                                    },
                                    {
                                        xtype: 'textfield',
                                        name: 'TechnicalChief',
                                        fieldLabel: '技术负责',
                                        emptyText: '技术负责人姓名',
                                        allowBlank: true,
                                        maxLength: 20
                                    },
                                    {
                                        xtype: 'textfield',
                                        name: 'QualityChief',
                                        fieldLabel: '质检负责',
                                        emptyText: '质检负责人姓名',
                                        allowBlank: true,
                                        maxLength: 20
                                    },
                                    {
                                        xtype: 'numberfield',
                                        name: 'Budget',
                                        fieldLabel: '建安预算',
                                        emptyText: '建安预算（万元）',
                                        allowBlank: true,
                                        minValue: 0,
                                        decimalPrecision: 2
                                    },
                                    {
                                        xtype: 'numberfield',
                                        name: 'ApprovedBudget',
                                        fieldLabel: '建安批复',
                                        emptyText: '建安批复（万元）',
                                        allowBlank: true,
                                        minValue: 0,
                                        decimalPrecision: 2
                                    },
                                    {
                                        xtype: 'panel',
                                        border: 0,
                                        layout:
                                        {
                                            type: 'hbox'
                                        },
                                        items:
                                        [
                                            {
                                                xtype: 'displayfield',
                                                fieldLabel: '项目附件'
                                            },
                                            grid_files
                                        ]
                                    }
                                ],
                                buttons:
                                [
                                    {
                                        text: '添加附件',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //获得表格控件
                                            var grid = Ext.getCmp('project-grid');

                                            //添加附件
                                            UploadFile
                                            (
                                                {
                                                    title: '上传附件',
                                                    mode: 1,
                                                    FileType: 1,
                                                    file_size_limit: attachment_file_size_limit,
                                                    file_size_limit_tip: attachment_file_size_limit_tip,
                                                    file_types: attachment_file_types,
                                                    file_types_description: attachment_file_types_description,
                                                    url: Common.MakeControllerUrl('FileUpload', 'UploadTempFile'),
                                                    callback: grid.onFileUploadSuccess
                                                }
                                            );
                                        }
                                    },
                                    {
                                        text: '确定',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //获得表单对象
                                            var form = this.up('form').getForm();

                                            //检查数据是否合法
                                            if (form.isValid())
                                            {
                                                //用于保存参数
                                                var params = [];

                                                //文件编号收集函数
                                                var fnCollectFileID = function (item, index, length)
                                                {
                                                    //添加文件编号
                                                    params.push(item.data.FileID);
                                                };

                                                //判定是否有附件
                                                if (store_files.getCount() > 0)
                                                {
                                                    //获得需要上传的附件
                                                    store_files.data.each(fnCollectFileID);
                                                }

                                                //提交数据
                                                form.submit
                                                (
                                                    {
                                                        url: Common.MakeAbsoluteUrl('Create'),
                                                        method: HttpMethod.Post,
                                                        params: { Attachments: params },
                                                        scope: this.up('window'),
                                                        submitEmptyText: false,
                                                        waitTitle: '请稍候',
                                                        waitMsg: '正在保存数据...',
                                                        success: function (form, action)
                                                        {
                                                            //处理提交错误
                                                            if (Common.HandleFormError(form, action))
                                                            {
                                                                try
                                                                {
                                                                    //清除附件数据
                                                                    store_files.removeAll(false);
                                                                    //重置数据
                                                                    form.reset();
                                                                    //隐藏窗体
                                                                    this.hide();
                                                                    //刷新数据
                                                                    store.load();
                                                                } catch (e)
                                                                {
                                                                    alert(e);
                                                                }
                                                            }
                                                        },
                                                        failure: function (form, action)
                                                        {
                                                            //清除附件数据
                                                            store_files.removeAll(false);

                                                            //处理提交错误
                                                            Common.HandleFormFailure(form, action);
                                                        }
                                                    }
                                                );
                                            }
                                        }
                                    },
                                    {
                                        text: '取消',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //清除附件数据
                                            store_files.removeAll(false);
                                            //获得表单对象
                                            var form = this.up('form').getForm();
                                            //重置数据
                                            form.reset();
                                            //隐藏窗体
                                            this.up('window').hide();
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                 );
            }

            //显示窗体
            this.win_create.show();
        },

        //处理编辑按钮点击事件
        onEditClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if (selection)
            {
                //判定窗体是否已经创建
                if (!this.win_edit)
                {
                    //创建窗体
                    this.win_edit = new Ext.Window
                    (
                        {
                            title: '编辑项目',
                            maximizable: false,
                            maximized: false,
                            width: 480,
                            height: 540,
                            autoScroll: false,
                            bodyBorder: false,
                            draggable: true,
                            isTopContainer: true,
                            modal: true,
                            resizable: false,
                            bodyPadding: 0,
                            closeAction: 'hide',
                            closable: false,
                            layout: 'fit',
                            fieldDefaults:
                            {
                                labelAlign: 'left',
                                labelWidth: 72,
                                anchor: '100%'
                            },
                            listeners:
                            {
                                //挂接显示事件
                                show: function (win, options)
                                {
                                    //查询表单对象对象
                                    var form = win.down('form').getForm();

                                    //加载数据
                                    form.load
                                    (
                                        {
                                            url: Common.MakeAbsoluteUrl('Details?id=' + win.current_row.ProjectID),
                                            waitMsg: '数据加载中...',
                                            failure: function (form, action)
                                            {
                                                //关闭窗体
                                                win.hide();

                                                //处理错误
                                                Common.HandleFormFailure(form, action);
                                            }
                                        }
                                    );
                                }
                            },
                            items:
                            [
                                {
                                    xtype: 'form',
                                    method: 'POST',
                                    waitMsgTarget: true,
                                    waitTitle: Common.ApplicationName,
                                    frame: false,
                                    bodyPadding: 12,
                                    bodyStyle: 'background-color:White;margin:0;',
                                    bodyBorder: false,
                                    border: 0,
                                    autoScroll: true,
                                    defaultType: 'textfield',
                                    autoScroll: false,
                                    fieldDefaults:
                                    {
                                        labelAlign: 'left',
                                        labelWidth: 72,
                                        anchor: '100%',
                                        msgTarget: 'qtip',
                                        autoFitErrors: true,
                                        disabledCls: ''
                                    },
                                    items:
                                    [
                                        {
                                            xtype: 'hiddenfield',
                                            name: 'ProjectID',
                                            allowBlank: false
                                        },
                                        {
                                            xtype: 'textfield',
                                            name: 'ProjectCode',
                                            fieldLabel: '项目编号',
                                            readOnly: true
                                        },
                                        {
                                            xtype: 'textfield',
                                            name: 'ProjectName',
                                            fieldLabel: '项目名称',
                                            emptyText: '80个字符',
                                            allowBlank: false,
                                            maxLength: 80
                                        },
                                        {
                                            xtype: 'combobox',
                                            name: 'CategoryID',
                                            itemId: 'cboCategory',
                                            fieldLabel: '项目类别',
                                            emptyText: '项目类别',
                                            store: store_master_category,
                                            displayField: 'Name',
                                            valueField: 'ID',
                                            queryMode: 'local',
                                            hideTrigger: false,
                                            forceSelection: true,
                                            typeAhead: true,
                                            autocomplete: true,
                                            allowBlank: false
                                        },
                                        {
                                            xtype: 'combobox',
                                            name: 'Roadway',
                                            itemId: 'cboRoadway',
                                            fieldLabel: '线路名称',
                                            emptyText: '项目线路名称',
                                            store: store_master_roadway,
                                            displayField: 'PinYin',
                                            valueField: 'Name',
                                            queryMode: 'local',
                                            hideTrigger: false,
                                            forceSelection: false,
                                            typeAhead: true,
                                            autocomplete: true,
                                            allowBlank: false,
                                            enableKeyEvents: true
                                        },
                                        {
                                            xtype: 'textfield',
                                            name: 'BeginStake',
                                            fieldLabel: '起点桩号',
                                            emptyText: '项目起点桩号',
                                            allowBlank: false,
                                            maxLength: 40
                                        },
                                        {
                                            xtype: 'textfield',
                                            name: 'EndStake',
                                            fieldLabel: '终点桩号',
                                            emptyText: '项目终点桩号',
                                            allowBlank: false,
                                            maxLength: 40
                                        },
                                        {
                                            xtype: 'textfield',
                                            name: 'GrantNumber',
                                            fieldLabel: '项目批号',
                                            emptyText: '项目文件批号',
                                            allowBlank: true,
                                            maxLength: 80
                                        },
                                        {
                                            xtype: 'datefield',
                                            name: 'StartDate',
                                            id: 'txtStartDate',
                                            fieldLabel: '开工日期',
                                            emptyText: '项目开工日期',
                                            format: 'Y/m/d',
                                            allowBlank: true,
                                            vtype: 'dateRange',
                                            endDateField: 'txtCompletionDate'
                                        },
                                        {
                                            xtype: 'datefield',
                                            name: 'CompletionDate',
                                            id: 'txtCompletionDate',
                                            fieldLabel: '竣工日期',
                                            emptyText: '项目竣工日期',
                                            format: 'Y/m/d',
                                            allowBlank: true,
                                            vtype: 'dateRange',
                                            beginDateField: 'txtStartDate'
                                        },
                                        {
                                            xtype: 'textfield',
                                            name: 'ProjectLeader',
                                            fieldLabel: '项目负责',
                                            emptyText: '项目负责人姓名',
                                            allowBlank: true,
                                            maxLength: 20
                                        },
                                        {
                                            xtype: 'textfield',
                                            name: 'TechnicalChief',
                                            fieldLabel: '技术负责',
                                            emptyText: '技术负责人姓名',
                                            allowBlank: true,
                                            maxLength: 20
                                        },
                                        {
                                            xtype: 'textfield',
                                            name: 'QualityChief',
                                            fieldLabel: '质检负责',
                                            emptyText: '质检负责人姓名',
                                            allowBlank: true,
                                            maxLength: 20
                                        },
                                        {
                                            xtype: 'numberfield',
                                            name: 'Budget',
                                            fieldLabel: '建安预算',
                                            emptyText: '建安预算（万元）',
                                            minValue: 0,
                                            allowBlank: true,
                                            decimalPrecision: 2
                                        },
                                        {
                                            xtype: 'numberfield',
                                            name: 'ApprovedBudget',
                                            fieldLabel: '建安批复',
                                            emptyText: '建安批复（万元）',
                                            minValue: 0,
                                            allowBlank: true,
                                            decimalPrecision: 2
                                        }
                                    ],
                                    buttons:
                                    [
                                        {
                                            text: '确定',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //获得表单对象
                                                var form = this.up('form').getForm();

                                                //检查数据是否合法
                                                if (form.isValid())
                                                {
                                                    //提交数据
                                                    form.submit
                                                (
                                                    {
                                                        url: Common.MakeAbsoluteUrl('Update'),
                                                        method: HttpMethod.Post,
                                                        scope: this.up('window'),
                                                        submitEmptyText: false,
                                                        waitTitle: '请稍候',
                                                        waitMsg: '正在保存数据...',
                                                        success: function (form, action)
                                                        {
                                                            //处理提交错误
                                                            if (Common.HandleFormError(form, action))
                                                            {
                                                                //重置数据
                                                                form.reset();
                                                                //隐藏窗体
                                                                this.hide();
                                                                //刷新数据
                                                                store.load();
                                                            }
                                                        },
                                                        failure: Common.HandleFormFailure
                                                    }
                                                );
                                                }
                                            }
                                        },
                                        {
                                            text: '取消',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //获得表单对象
                                                var form = this.up('form').getForm();
                                                //重置数据
                                                form.reset();
                                                //隐藏窗体
                                                this.up('window').hide();
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    );
                }

                //设置当前行
                this.win_edit.current_row = selection.data;

                //显示窗体
                this.win_edit.show();
            }
        },

        //处理提交按钮点击事件
        onSubmitClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection();

            //判断是否选中行
            if (selection.length > 0)
            {
                //询问是否确定删除
                Ext.Msg.show
                (
                    {
                        title: '提交批准',
                        width: 320,
                        height: 240,
                        msg: '确定要将工程项目"' + selection[0].data.ProjectName + '"提交批准吗?',
                        buttons: Ext.Msg.YESNO,
                        icon: Ext.Msg.QUESTION,
                        fn: function (button)
                        {
                            //判断是否确定删除 
                            if (button == 'yes')
                            {
                                //创建请求对象
                                Ext.Ajax.request
                                (
                                    {
                                        url: Common.MakeAbsoluteUrl('ExecuteSubmit'),
                                        method: HttpMethod.Post,
                                        params:
                                        {
                                            projectID: selection[0].data.ProjectID
                                        },
                                        callback: function (options, success, response)
                                        {
                                            //处理错误
                                            if (Common.HandleError(options, success, response))
                                            {
                                                //加载数据
                                                store.load();
                                            }
                                        }
                                    }
                                );
                            }
                        }
                    }
                );
            }
        },

        //处理查看提交按钮点击事件
        onViewClick: function ()
        {
            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if (selection)
            {
                //查看工程详细信息
                window.open(Common.MakeAbsoluteUrl('ViewProject?') + 'projectID=' + selection.data.ProjectID);
            }
        },

        //处理上传按钮点击事件
        onUploadClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if (selection)
            {
                //上传附件
                this.onManageAttachment(selection.data.ProjectID);
            }
        }
    }
);

//挂接环境准备完毕事件
Ext.onReady
(
    function ()
    {
        try
        {
            //初始化快速提示
            Ext.QuickTips.init();

            //设置状态保存提供者（当前设置为Cookie）
            Ext.state.Manager.setProvider( Ext.create( 'Ext.state.CookieProvider', { path: _Current_ActionUrl } ) );

            //初始化附加验证类型
            Common.InitAdditionalValidation();

            //创建线路存储对象
            store_master_roadway = Ext.create
            (
                'Ext.data.Store',
                {
                    fields: ['Name', 'PinYin'],
                    data: master_roadway
                }
            );

            //创建分类
            store_master_category = Ext.create
            (
                'Ext.data.Store',
                {
                    fields: ['ID', 'Name'],
                    data: master_category
                }
            );

            //创建文件列表
            store_files = Ext.create
            (
                'Ext.data.ArrayStore',
                {
                    autoDestroy: true,
                    model: 'UploadFileModel'
                }
            );

            //创建附件数据存储对象
            store_attachment = Ext.create
            (
                'Ext.data.Store',
                {
                    root: 'data',
                    autoLoad: false,
                    remoteFilter: true,
                    remoteSort: true,
                    model: 'ProjectAttachmentModel',
                    proxy:
                    {
                        type: 'ajax',
                        batchActions: false,
                        reader:
                        {
                            type: 'json',
                            root: 'data'
                        },
                        listeners:
                        {
                            exception: function (proxy, response, operation, options)
                            {
                                //处理错误
                                Common.HandleProxyError(proxy, response, operation, options);
                            }
                        }
                    }
                }
            );

            //创建数据存储对象
            var store = Ext.create
            (
                'Ext.data.Store',
                {
                    pageSize: 20,
                    autoSync: true,
                    autoLoad: true,
                    root: 'data',
                    remoteFilter: true,
                    remoteSort: true,
                    model: 'ProjectModal',
                    proxy:
                    {
                        type: 'ajax',
                        batchActions: false,
                        url: Common.MakeAbsoluteUrl('SubmitSelect'),
                        reader:
                        {
                            type: 'json',
                            root: 'data'
                        },
                        listeners:
                        {
                            exception: function (proxy, response, operation, options)
                            {
                                //处理错误
                                Common.HandleProxyError(proxy, response, operation, options);
                            }
                        }
                    }
                }
            );

            Ext.create
            (
                'Project.Grid',
                {
                    store: store,
                    stateful: true,
                    stateId: 'project-submit-state-grid',
                    height: 600,
                    width: '100%',
                    id: 'project-grid',
                    title: '',
                    renderTo: 'data-grid',
                    columnLines: true,
                    viewConfig:
                    {
                        stripeRows: true,
                        loadingText: '数据加载中...',
                        singleSelect: true
                    },
                    //创建分页工具栏
                    bbar: Ext.create
                    (
                        'Ext.PagingToolbar',
                        {
                            store: store,
                            displayInfo: true,
                            displayMsg: '第 {0} - 第 {1} 条记录，共 {2} 条记录',
                            emptyMsg: ''
                        }
                    ),
                    columns:
                    [
                        {
                            text: '所属单位',
                            width: 160,
                            sortable: true,
                            align: 'left',
                            dataIndex: 'Organization',
                            hideable: false
                        },
                        {
                            text: '项目编号',
                            width: 160,
                            sortable: true,
                            align: 'left',
                            dataIndex: 'ProjectCode',
                            hideable: false
                        },
                        {
                            text: '项目名称',
                            width: 160,
                            minWidth: 160,
                            flex: 1,
                            sortable: true,
                            align: 'left',
                            dataIndex: 'ProjectName',
                            hideable: false
                        },
                        {
                            text: '项目类别',
                            width: 120,
                            sortable: true,
                            align: 'left',
                            dataIndex: 'Category'
                        },
                        {
                            text: '项目状态',
                            width: 80,
                            sortable: true,
                            align: 'center',
                            dataIndex: 'State',
                            renderer: Common.RenderProjectState
                        },
                        {
                            text: '线路名称',
                            width: 120,
                            sortable: true,
                            align: 'left',
                            dataIndex: 'Roadway'
                        },
                        {
                            text: '起点桩号',
                            width: 80,
                            sortable: true,
                            align: 'left',
                            dataIndex: 'BeginStake'
                        },
                        {
                            text: '终止桩号',
                            width: 80,
                            sortable: true,
                            align: 'left',
                            dataIndex: 'EndStake'
                        },
                        {
                            text: '项目批号',
                            width: 80,
                            sortable: true,
                            align: 'left',
                            dataIndex: 'GrantNumber'
                        },
                        {
                            text: '开工日期',
                            width: 80,
                            sortable: false,
                            align: 'right',
                            renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                            dataIndex: 'StartDate'
                        },
                        {
                            text: '竣工日期',
                            width: 80,
                            sortable: false,
                            align: 'right',
                            renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                            dataIndex: 'CompletionDate'
                        },
                        {
                            text: '项目负责',
                            width: 80,
                            sortable: true,
                            align: 'left',
                            dataIndex: 'ProjectLeader'
                        },
                        {
                            text: '技术负责',
                            width: 80,
                            sortable: true,
                            align: 'left',
                            dataIndex: 'TechnicalChief'
                        },
                        {
                            text: '质检负责',
                            width: 80,
                            sortable: true,
                            align: 'left',
                            dataIndex: 'QualityChief'
                        },
                        {
                            text: '建安预算（万）',
                            width: 100,
                            sortable: true,
                            align: 'right',
                            renderer: Ext.util.Format.numberRenderer('0,000.00'),
                            dataIndex: 'Budget'
                        },
                        {
                            text: '建安批复（万）',
                            width: 100,
                            sortable: true,
                            align: 'right',
                            renderer: Ext.util.Format.numberRenderer('0,000.00'),
                            dataIndex: 'ApprovedBudget'
                        },
                        {
                            text: '创建日期',
                            width: 80,
                            sortable: true,
                            align: 'right',
                            renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                            dataIndex: 'CreateDate'
                        }
                    ]
                }
            );

            $(window).bind
            (
                "resize",
                function ()
                {
                    //调整表格宽度
                    Common.AdjustGridWidth('project-grid', 'body_content');
                }
            );

            //调整表格宽度
            Common.AdjustGridWidth('project-grid', 'body_content');
        }
        catch (e)
        {
            //提示错误信息
            Ext.MessageBox.show
            (
                {
                    title: Common.ApplicationName,
                    msg: e,
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR
                }
            );
        }
    }
);
